﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
/*
Developed By: Niraj Kumar Rai
Developed On: 12-Aug-2011
Description: This code Delete the project and download the project content
*/
 namespace SiteProjects.Layouts.DeleteProject  
{
    public partial class DeleteProject : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
                SPSite site = SPContext.Current.Site;
                siteName.Text = site.Url;               
                //Call the Download content method
                DownloadContent();
                //Find the user is Author?
                if (!isAuthor())
                {
                    Response.Redirect(SPContext.Current.Site.RootWeb.Url, false);                   
                }
            
        }

        public void DownloadContent()
        {
            try
            {
                string downloadContentUrl = string.Empty;
                SPSite site = SPContext.Current.Site;

                //this script used to pop up open in center and call the download aspx page
                string strScript = @"function openCenteredWindow(url) {
                       var width = 10;
                       var height = 10;
                       var left = parseInt((screen.availWidth/2) -100);
                       var top = parseInt((screen.availHeight/2) -100);
                       var windowFeatures = 'width=' + width + ',height=' + height + ',status,resizable,left=' + left + ',top=' + top + 'screenX=' + left + ',screenY=' + top;
                       myWindow = window.open(url, 'subWind', windowFeatures);
                       }";
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CallWindow", strScript, true);
                siteName.Text = site.Url;
                downloadContentUrl = site.Url + "/_layouts/DownloadContent/Download.aspx";
                btnDownloadcontent.Attributes.Add("onClick", "javascript:openCenteredWindow('" + downloadContentUrl + "')");
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        private bool isAuthor()
        {
            SPSite WebSite = SPContext.Current.Site;
            bool isAuthor = false;

            string siteAuthor = string.Empty;
            string curUser = string.Empty;
            try
            {
                using (SPSite site = new SPSite(WebSite.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        siteAuthor = web.Author.ToString();
                        siteAuthor = siteAuthor.Replace("i:0#.w|", "");
                        siteAuthor = siteAuthor.Replace("i:0#.f|sqlmember|", "");

                        curUser = web.CurrentUser.ToString();
                        curUser = curUser.Replace("i:0#.w|", "");
                        curUser = curUser.Replace("i:0#.f|sqlmember|", "");

                        //if login user is site author then delete button will be visible.
                        if (siteAuthor == curUser)
                            isAuthor = true;
                    }
                }
            }
            catch { }
            return isAuthor;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
                SPSiteCollection siteCollections = webApplication.Sites;
              
                string prpjectRootUrl = SPContext.Current.Site.RootWeb.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, "/");
                for (int i = 0; i < siteCollections.Count; i++)
                {
                    //Check site name is exist in collection   
                    if (SPContext.Current.Site.ServerRelativeUrl.ToUpper() == "/" + siteCollections.Names[i].ToUpper())
                    {   
                        //Delete the existing  user project
                        siteCollections.Delete(SPContext.Current.Site.ServerRelativeUrl);
                        Response.Redirect(prpjectRootUrl,false);
                    }
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(SPContext.Current.Site.RootWeb.Url,false);
        }
    }
}
